<?php

namespace App\Repos;

use App\Models\Invite;
use App\Models\User;
use App\Models\UserSetting;
use App\Models\Intend;
use App\Models\InvitePoint;
use App\Models\UserPoint;
use App\Models\UserCoupon;
use DB;

class InviteRepo 
{
    public static function getPages($where=[],$num=10)
    {
    	$list  = DB::table('invites as i')
					->select(['i.id','u.nickname','u.face_score','u.sex','u.zhiye','u.avatar','i.point','i.created_at','i.to_user_id','i.from_user_id','i.status','u.avatar','u.wx_avatar',DB::raw('TIMESTAMPDIFF(YEAR, u.birthday, CURDATE()) AS age'),DB::raw('left(truename, 1) as chenghu')])
					->leftJoin('users as u','u.id','=','i.to_user_id')
					->where($where)
					->paginate($num);
    	
    	return $list;
    }

    public static function getToMyPages($where=[],$num=10,$orderby='id')
    {
        $list  = DB::table('invites as i')
                    ->select(['i.id','u.nickname','u.face_score','u.sex','u.zhiye','u.avatar','i.point','i.created_at','i.to_user_id','i.from_user_id','i.status','u.avatar',DB::raw('TIMESTAMPDIFF(YEAR, u.birthday, CURDATE()) AS age'),DB::raw('left(truename, 1) as chenghu')])
                    ->leftJoin('users as u','u.id','=','i.from_user_id')
                    ->where($where)
                    ->orderby($orderby,'desc')
                    ->paginate($num);
        
        return $list;
    }

    public static function save($data)
    {
        $rs['status'] = true;
        $where['user_id']    = $data['from_user_id'];
        $where['point'] = $data['point'];

        // 看看目标用户是否自动拒绝不符合条件的
        
        $us = UserSetting::where(['user_id'=>$data['to_user_id']])
                            ->first();

        if($us->auto_reject) {
            $rs['msg']    = '抱歉，您的条件不符合对方的要求:';
            
            $intentd = Intend::where('user_id',$data['to_user_id'])->first();
            //dd($intentd);
            if($intentd) {
                $user = User::find($data['from_user_id']);

                if($user->sex!=$intentd->sex) {
                    $rs['msg'] .= '性别'; 
                    $rs['status'] = false;
                    return $rs;
                }
                if($intentd->face_score) {
                    
                    if($user->face_score<$intentd->face_score){
                        $rs['msg'] .= '颜值';
                        $rs['status'] = false;    
                        return $rs;    
                    }                
                }

                if($intentd->height) {
                    $height = explode(',', $intentd->height);

                    if($user->height<$height[0] || $user->height>$height[1]){
                        $rs['msg'] .= '身高';
                        $rs['status'] = false;    
                        return $rs;    
                    }                
                }

                if($intentd->age) {
                    $age = explode(',', $intentd->age);
                    $user_age = explode('-', $user->birthday);
                    //dd($age,$user_age[0]);
                    if($user_age[0]<$age[0] || $user_age[0]>$age[1]){
                        $rs['msg'] .= '年龄';
                        $rs['status'] = false;    
                        return $rs;    
                    }                
                }

                if($intentd->is_married==1 && $user->is_married==1) {    $rs['msg'] .= '婚姻情况';   
                        $rs['status'] = false;    
                        return $rs;                                
                }

                if($intentd->is_havebaby==1 && $user->is_havebaby==1) {    $rs['msg'] .= '是否有孩子';  
                        $rs['status'] = false;    
                        return $rs;                                
                }

                if($intentd->is_wantbaby != $user->is_wantbaby) { 
                        $rs['msg'] .= '是否要宝宝';       
                        $rs['status'] = false;    
                        return $rs;                                
                }

                if($intentd->is_smoke==1 && $user->is_smoke==1) { 
                        $rs['msg'] .= '是否要吸烟';      
                        $rs['status'] = false;    
                        return $rs;                                
                }

                if($intentd->is_ill==1 && $user->is_ill==1) { 
                        $rs['msg'] .= '健康状态';      
                        $rs['status'] = false;    
                        return $rs;                                
                }
                //dd($intentd->xueli_rank,$user->xueli_rank);
                if($intentd->xueli_rank > $user->xueli_rank) {
                    $rs['msg'] .= '学历'; 
                    $rs['status'] = false;
                    return $rs;
                }

                if($intentd->salary_rank > $user->salary_rank) {
                    $rs['msg'] .= '收入'; 
                    $rs['status'] = false;
                    return $rs;
                }
            }    

        }                    

        
        $flag = UserRepo::usePoint($where);
        if($flag['status'] == false) {
            $rs['status'] = false;
            $rs['msg'] = $flag['msg'];
            return $rs;
        }

        $data['status'] = 1;
    	$ret = Invite::create($data);
        $ip['coupon_point'] = array_get($flag,'data.coupon_point');
        $ip['point']        = array_get($flag,'data.point');
        $ip['user_id']      = $data['from_user_id'];
        $ip['invite_id']    = $ret->id;
        InvitePoint::create($ip);
    	return $rs;
    }

    public static function find($where=[])
    {
        $invite =  Invite::where($where)
                    ->orderBy('id','desc')
                    ->first();
        if($invite) {
            $invite->is_read = 1;
            $invite->save();
            return $invite;     
        } 
        return null;  
       
                  
    }

    public static function findOne($where=[])
    {
        $invite =  Invite::where($where)
                    ->orderBy('id','desc')
                    ->first();
       
        return $invite;     
         
       
                  
    }

    public static function update($where=[],$data)
    {
        $invite = Invite::where($where)->first();
        if($invite->status == $data['status']) return true;
        
        if($data['status']=='-1') {
            // 被拒绝了要返还积分 状态为3

            $ip = InvitePoint::where(['invite_id'=>$where['id']])
                            ->first();
            if($ip) {
                $ip->status = 3;
                $ip->save();     
            }                
                           
            $user = User::find($ip->user_id);    
            $user->point        += object_get($ip,'point'); 
            $user->coupon_point += object_get($ip,'coupon_point');
            $user->save();

            // 被拒退还积分的记录
            if(object_get($ip,'point')>0){
                $up['user_id'] = $user->id;
                $up['point']   = $ip->point;
                $up['type']    = 3;
                $up['remark']  = '邀约被拒退还积分:'.$ip->point;
                UserPoint::create($up);    
            }
            
            if(object_get($ip,'coupon_point')>0) {
                // 被拒退还代金券
                $uc['user_id']      = $user->id;
                $uc['coupon_point'] = $ip->coupon_point;
                $uc['type'] = 3;
                $uc['remark'] = '邀约被拒退还代金券积分:'.$ip->coupon_point;
                UserCoupon::create($uc);    
            }
                       
            
        } elseif ($data['status']=='100') {
            // 同意了要修改状态为2
            InvitePoint::where(['invite_id'=>$where['id']])->update(['status'=>2]);
        }
        Invite::where($where)->update($data);
        return true;

    }
}
